package com.example.service;

import com.example.model.Member;
import com.example.model.MemberQuery;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MemberService {

    private Integer executeCountPrivate = 0;
    public Integer executeCountPublic = 0;

    private static Integer executeStaticCountPrivate = 0;
    public static Integer executeStaticCountPublic = 0;


    public Member getMemberById(Integer id){
        executeStaticCountPrivate += 1;
        executeStaticCountPublic += 1;
        return memberList.stream()
                .filter(member -> member.id().equals(Long.valueOf(id)))
                .findFirst().orElse(null);
    }

    public Member getMember(Long id){
        return memberList.stream()
                .filter(member -> member.id().equals(id))
                .findFirst().orElse(null);
    }

    public List<Member> getMembers(Integer status){
        return memberList.stream()
                .filter(member -> member.status().equals(status))
                .toList();
    }

    public List<Member> getMembers(MemberQuery memberQuery){
        return memberList.stream()
                .filter(member -> {
                    if(memberQuery.id() != null && !memberQuery.id().equals(member.id())){
                        return false;
                    }
                    if(memberQuery.status() != null && !memberQuery.status().equals(member.status())){
                        return false;
                    }
                    if(memberQuery.name() != null && !member.name().contains(memberQuery.name())){
                        return false;
                    }
                    if(memberQuery.mobilePhone() != null && !member.mobilePhone().contains(memberQuery.mobilePhone())){
                        return false;
                    }
                    return true;
                })
                .toList();
    }

    public Long getMemberId(String name){
        return memberList.stream()
                .filter(member -> member.name().equals(name))
                .map(Member::id)
                .findFirst().orElse(null);
    }

    @PostConstruct
    public void init(){
        memberList.add(new Member(1L, "member1", "member1@naver.com", 1));
        memberList.add(new Member(2L, "member2", "member2@naver.com", 1));
        memberList.add(new Member(3L, "member3", "member3@naver.com", 1));
        memberList.add(new Member(4L, "member4", "member4@naver.com", 1));
        memberList.add(new Member(5L, "member5", "member5@naver.com", 1));
        memberList.add(new Member(6L, "member6", "member6@naver.com", 1));
        memberList.add(new Member(7L, "member7", "member7@naver.com", 2));
        memberList.add(new Member(8L, "member8", "member8@naver.com", 2));
        memberList.add(new Member(9L, "member9", "member9@naver.com", 2));
    }

    private static List<Member> memberList = new ArrayList<>();
}
